package com.xudow.app.ui.adapter.user;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.activeshare.edu.ucenter.models.order.AgencyOrderDetail;
import com.activeshare.edu.ucenter.models.order.AgencyOrderItemDetail;
import com.daimajia.swipe.SwipeLayout;
import com.xudow.app.Config;
import com.xudow.app.R;
import com.xudow.app.ui.user.OrdersActivity;
import com.xudow.app.util.CourseTypeUtils;
import com.xudow.app.util.ImageUtils;

import java.util.List;

/**
 * OrderAdapter
 *
 * @author lukecheng
 */
public class OrderAdapter extends RecyclerView.Adapter {

    public interface OnRecyclerViewListener {
        void onItemClick(int position);

        boolean onItemLongClick(int position);

        void onDelete(int position);
    }

    private OnRecyclerViewListener onRecyclerViewListener;

    public void setOnRecyclerViewListener(OnRecyclerViewListener onRecyclerViewListener) {
        this.onRecyclerViewListener = onRecyclerViewListener;
    }

    /**
     * 订单
     */
    private List<AgencyOrderDetail> orders;

    private Context context;

    public OrderAdapter(Context context, List<AgencyOrderDetail> orders) {
        this.context = context;
        this.orders = orders;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_orderlist, null);

        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        view.setLayoutParams(lp);

        return new OrderViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        OrderViewHolder viewHolder = (OrderViewHolder) holder;

        AgencyOrderDetail order = orders.get(position);
        AgencyOrderItemDetail itemDetail = order.getItems().get(0);

        viewHolder.address.setText(itemDetail.getCourse().getSchoolAdress());
        viewHolder.agencyName.setText(itemDetail.getCourse().getAgencyName());
        viewHolder.coursePrice.setText("￥" + itemDetail.getPrice());
        viewHolder.courseRealPrice.setText("￥" + itemDetail.getCourse().getCoursePrice());
        viewHolder.courseName.setText(itemDetail.getCourse().getName());
        viewHolder.courseType.setText(CourseTypeUtils.getCourseType(itemDetail.getCourse().getType()));

        switch (order.getStatus()) {
            case OrdersActivity.ORDER_STATUS_PAID:
                viewHolder.payStatus.setText("已支付");
                viewHolder.payStatus.setBackgroundResource(R.mipmap.pay_status_paid_bg);
                viewHolder.payedLy.setVisibility(View.VISIBLE);
                viewHolder.unpayedLy.setVisibility(View.GONE);
                break;
            case OrdersActivity.ORDER_STATUS_UNPAY:
                viewHolder.payStatus.setText("待支付");
                viewHolder.payStatus.setBackgroundResource(R.mipmap.pay_status_bg);
                viewHolder.payedLy.setVisibility(View.GONE);
                viewHolder.pay_order.setVisibility(View.INVISIBLE);
                break;
            case OrdersActivity.ORDER_STATUS_EXPIRED:
                viewHolder.payStatus.setText("已过期");
                viewHolder.payStatus.setBackgroundResource(R.mipmap.pay_status_bg);
                viewHolder.payedLy.setVisibility(View.GONE);
                viewHolder.pay_order.setVisibility(View.INVISIBLE);
                break;
            case OrdersActivity.ORDER_STATUS_DELETED:
                viewHolder.payStatus.setText("已删除");
                viewHolder.payStatus.setBackgroundResource(R.mipmap.pay_status_bg);
                viewHolder.payedLy.setVisibility(View.GONE);
                viewHolder.pay_order.setVisibility(View.INVISIBLE);
                break;
            default:
                viewHolder.payStatus.setText("未知");
                viewHolder.payStatus.setBackgroundResource(R.mipmap.pay_status_bg);
                viewHolder.payedLy.setVisibility(View.GONE);
                viewHolder.pay_order.setVisibility(View.INVISIBLE);
                break;
        }

        String imageUrl = String.format(Config.IMAGE_LOAD_URL_PARAMS, itemDetail.getCourse().getThumbnail());
        ImageUtils.loadImage(context, viewHolder.agencyImage, imageUrl, R.mipmap.course_def_img);


        viewHolder.root.addSwipeListener(new SwipeLayout.SwipeListener() {
            @Override
            public void onStartOpen(SwipeLayout swipeLayout) {

            }

            @Override
            public void onOpen(SwipeLayout swipeLayout) {
                LinearLayout bottomWrapper = (LinearLayout) swipeLayout.findViewById(R.id.bottom_wrapper);
                LinearLayout deleteItem = (LinearLayout) swipeLayout.findViewById(R.id.delete_item);
                ImageView deleteImage = (ImageView) swipeLayout.findViewById(R.id.delete_image);
                TextView deleteText = (TextView) swipeLayout.findViewById(R.id.delete_text);

                View.OnClickListener listener = new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (null != onRecyclerViewListener) {
                            onRecyclerViewListener.onDelete(position);
                        }
                    }
                };

                bottomWrapper.setOnClickListener(listener);
                deleteItem.setOnClickListener(listener);
                deleteImage.setOnClickListener(listener);
                deleteText.setOnClickListener(listener);
            }

            @Override
            public void onStartClose(SwipeLayout swipeLayout) {

            }

            @Override
            public void onClose(SwipeLayout swipeLayout) {

            }

            @Override
            public void onUpdate(SwipeLayout swipeLayout, int i, int i1) {

            }

            @Override
            public void onHandRelease(SwipeLayout swipeLayout, float v, float v1) {

            }
        });

        //TODO:计算距离
    }

    @Override
    public int getItemCount() {
        return orders.size();
    }

    class OrderViewHolder extends RecyclerView.ViewHolder
            implements View.OnClickListener, View.OnLongClickListener {

        private LinearLayout detailItem;
        public TextView agencyName;
        public TextView payStatus;
        public ImageView agencyImage;
        public TextView courseName;
        public TextView courseType;
        public TextView address;
        public TextView distance;
        private TextView coursePrice;
        private TextView courseRealPrice;

        public SwipeLayout root;
        RelativeLayout payedLy,unpayedLy;
        TextView cancle_order,pay_order;
        public OrderViewHolder(View itemView) {
            super(itemView);

            detailItem = (LinearLayout) itemView.findViewById(R.id.detail_item);
            detailItem.setOnClickListener(this);
            detailItem.setOnLongClickListener(this);

            agencyName = (TextView) itemView.findViewById(R.id.agency_name);
            payStatus = (TextView) itemView.findViewById(R.id.pay_status);
            courseName = (TextView) itemView.findViewById(R.id.course_name);
            courseType = (TextView) itemView.findViewById(R.id.course_type);
            address = (TextView) itemView.findViewById(R.id.address);
            distance = (TextView) itemView.findViewById(R.id.distance);
            coursePrice = (TextView) itemView.findViewById(R.id.course_price);
            courseRealPrice = (TextView) itemView.findViewById(R.id.course_real_price);
            agencyImage = (ImageView) itemView.findViewById(R.id.agency_image);
            root = (SwipeLayout) itemView.findViewById(R.id.root);

            payedLy=(RelativeLayout)itemView.findViewById(R.id.payed_layout);
            unpayedLy=(RelativeLayout)itemView.findViewById(R.id.unpayed_layout);
            cancle_order=(TextView)itemView.findViewById(R.id.cancel_tv);
            pay_order=(TextView)itemView.findViewById(R.id.pay_tv);
        }

        @Override
        public void onClick(View v) {
            if (null != onRecyclerViewListener) {
                onRecyclerViewListener.onItemClick(this.getAdapterPosition());
            }
        }

        @Override
        public boolean onLongClick(View v) {
            if (null != onRecyclerViewListener) {
                return onRecyclerViewListener.onItemLongClick(this.getAdapterPosition());
            }

            return false;
        }
    }
}
